package com.oa.admin.service.impl;

import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.PurchaseRequestAddDTO;
import com.oa.admin.dto.PurchaseRequestListDTO;
import com.oa.admin.entity.PurchaseDetails;
import com.oa.admin.service.IPurchaseDetailsService;
import com.oa.admin.vo.PurchaseRequestDetailsVO;
import com.oa.admin.vo.PurchaseRequestListVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.PurchaseRequest;
import com.oa.admin.mapper.PurchaseRequestMapper;
import com.oa.admin.service.IPurchaseRequestService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.service.IApplyApprovalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 采购申请表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class PurchaseRequestServiceImpl extends ServiceImpl<PurchaseRequestMapper, PurchaseRequest> implements IPurchaseRequestService {

    private final PurchaseRequestMapper purchaseRequestMapper;

    @Autowired
    public PurchaseRequestServiceImpl(PurchaseRequestMapper purchaseRequestMapper) {
        this.purchaseRequestMapper = purchaseRequestMapper;
    }

    @Autowired
    private IPurchaseDetailsService purchaseDetailsService;

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Override
    @CommonService
    public PageResult<PurchaseRequest> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<PurchaseRequest> list = purchaseRequestMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<PurchaseRequest> page = new Page<>(pageNum, pageSize);
            //查询
            List<PurchaseRequest> list = purchaseRequestMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public PurchaseRequestDetailsVO findPurchaseRequestById(String id){
        return purchaseRequestMapper.findPurchaseRequestDetails(id);
    }

    @Override
    @CommonService
    public PageResult<PurchaseRequestListVO> findPurchaseRequestList(PurchaseRequestListDTO purchaseRequestListDTO) {
        Page page = purchaseRequestListDTO.getPage();
        return new PageResult<>(purchaseRequestMapper.findPurchaseRequestList(page, purchaseRequestListDTO), page.getPages());
    }

    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp add(PurchaseRequestAddDTO purchaseRequestAddDTO) throws BusinessException, DataIsEmptyException {

        PurchaseRequest purchaseRequest = new PurchaseRequest();
        BeanUtils.copyProperties(purchaseRequestAddDTO, purchaseRequest);
        purchaseRequest.setApplyTime(new Date());
        if(insert(purchaseRequest)){
            List<PurchaseDetails> purchaseDetails = new ArrayList<>();
            purchaseRequestAddDTO.getPurchaseDetailsList().forEach(d -> {
                PurchaseDetails purchaseDetails1 = new PurchaseDetails();
                BeanUtils.copyProperties(d, purchaseDetails1);
                purchaseDetails1.setPurchaseApplyId(purchaseRequest.getId());
                purchaseDetails1.setId(null);
                purchaseDetails.add(purchaseDetails1);
            });
            if(purchaseDetailsService.insertBatch(purchaseDetails)){
                ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
                applyApprovalAddDTO.setApplyId(purchaseRequest.getId());
                applyApprovalAddDTO.setApprovalUserId(purchaseRequestAddDTO.getProposer());
                if(purchaseRequestAddDTO.getPurchaseType() == 1){
                    applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.PURCHASE.getId());
                    applyApprovalAddDTO.setApplyApprovalName(purchaseRequestAddDTO.getPurchaseName() + ProcessDefineEnum.PURCHASE.getText());

                    return applyApprovalService.addApproval(applyApprovalAddDTO);
                }else{
                    applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.TEMPORARY_PURCHASE.getId());
                    applyApprovalAddDTO.setApplyApprovalName(purchaseRequestAddDTO.getPurchaseName() + ProcessDefineEnum.TEMPORARY_PURCHASE.getText());

                    return applyApprovalService.addApproval(applyApprovalAddDTO);
                }
            }
        }
        throw new BusinessException("提交采购申请失败!");
    }

    @Override
    public void export(PurchaseRequestListDTO purchaseRequestListDTO, HttpServletResponse response) {
        List<PurchaseRequestListVO> purchaseRequestListVOS;
        if(purchaseRequestListDTO.getPage() != null){
            Page page = purchaseRequestListDTO.getPage();
            purchaseRequestListVOS = purchaseRequestMapper.findPurchaseRequestList(page, purchaseRequestListDTO);
        }else{
            purchaseRequestListVOS = purchaseRequestMapper.findPurchaseRequestList(new Page<>(), purchaseRequestListDTO);
        }
        String[] rows = {"申请ID", "采购ID", "审核时间", "采购名称", "采购金额", "审核状态", "备注"};
        try {
            ExcelUtil.downloadExcel("采购申请列表" + DateUtil.getDayMonth(), rows, purchaseRequestListVOS, "采购申请列表" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
